package com.simon.mctm.biz.sys.controller;


import com.simon.mctm.biz.sys.bean.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.CollectionUtils;

import com.simon.mctm.base.response.DoResponse;
import com.simon.mctm.base.response.PageResponse;
import com.simon.mctm.exception.CheckException;
import com.simon.mctm.exception.OperationFailedException;
import com.simon.mctm.token.TokenValidate;
import com.simon.mctm.base.PageResult;
import com.simon.mctm.biz.sys.bean.Dept;
import com.simon.mctm.biz.sys.service.DeptService;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import java.util.List;


/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author simon
 * @since 2018-06-15
 */
@RestController
@Api(description = "部门控制器")
@RequestMapping("/sys/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @ApiOperation(value = "添加部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "dept", value = "部门对象", required = true, dataType = "Dept")
    })
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @TokenValidate
    public DoResponse<?> addDept(@RequestBody Dept dept){
        Dept temp = deptService.selectOne(
                new EntityWrapper<Dept>().eq("simplename",dept.getSimplename())
                .eq("pid",dept.getPid())
        );
        if (temp != null) {
            throw new OperationFailedException("该部门已经存在");
        }
        boolean res = deptService.insert(dept);
        if(res){
            return new DoResponse<>();
        }else{
            throw new OperationFailedException("添加部门失败");
        }
    }


    @ApiOperation(value = "获取指定部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "部门id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> getDept(@RequestParam Integer id) {
        Dept dept = deptService.selectById(id);
        if (dept != null) {
            return new DoResponse<>(dept);
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "获取部门列表")
    @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> getAllDept() {
        EntityWrapper<Dept> ew = new EntityWrapper<>();
        List<Dept> deptList = deptService.selectList(ew);
        if (!CollectionUtils.isEmpty(deptList)) {
            return new DoResponse<>(deptList);
        } else {
            throw new OperationFailedException();
        }
    }


    @ApiOperation(value = "查询部门列表,分页形式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "pageNum", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", dataType = "int", defaultValue = "10")
    })
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    @TokenValidate
    public PageResponse<?> getDeptPagedList(@RequestParam String simplename, @RequestParam Integer pageNum,
                                            @RequestParam Integer pageSize) {
        if (pageNum == 0 || pageSize == 0) {
            throw new CheckException("页码和单页条目必输");
        }
        Page<Dept> deptPage = deptService.selectPage(
                new Page<>(pageNum, pageSize),
                new EntityWrapper<Dept>().eq("simplename",simplename)
        );
        List<Dept> deptList = deptPage.getRecords();
        PageResult bPage = deptService.calPage(simplename,pageSize);
        if (!CollectionUtils.isEmpty(deptList)) {
            return new PageResponse<>(deptList, deptList.size(), bPage.getTotalElements(), bPage.getTotalPage());
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "删除部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "部门id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @TokenValidate
    public DoResponse<?> deleteDept(@RequestParam Integer id) {
        boolean res = deptService.deleteById(id);
        if (res) {
            return new DoResponse<>();
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "更新部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "部门id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "dept", value = "部门对象", required = true, dataType = "Dept")
    })
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @TokenValidate
    public DoResponse<?> updateDept(@RequestParam Integer id, @RequestBody Dept dept) {
        Dept temp = deptService.selectById(id);
        if (temp == null) {
            throw new OperationFailedException("该部门不存在");
        }
        Dept temp1 = deptService.selectOne(
                new EntityWrapper<Dept>().eq("simplename",dept.getSimplename())
                        .eq("pid",dept.getPid())
        );
        if (temp1 != null) {
            throw new OperationFailedException("该部门已经存在,不允许简称重复");
        }
        boolean res = deptService.updateById(dept);
        if (res) {
            return new DoResponse<>();
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "查询子部门", notes = "根据pid获取子部门信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌",  paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "pid", value = "上级部门id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/sublist", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> sublistDept(@RequestParam Integer pid) {
        List<Dept> deptList = deptService.selectList(
                new EntityWrapper<Dept>().eq("pid",pid)
        );
        if (deptList != null) {
            return new DoResponse<>(deptList);
        } else {
            throw new OperationFailedException();
        }
    }
}

